
package security.beans;

import Entity.Rol;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import login.beans.ConectionBean;
import org.primefaces.event.TransferEvent;
import org.primefaces.model.DualListModel;
import security.business.RolBusiness;

/**
 *
 * @author JuanFelipe
 */
@ManagedBean
@RequestScoped
public class CreateRolBean {
    private DualListModel< String > privileges;
    private Rol currentRol;
    private Connection conn;
    
    public CreateRolBean( ) {
        
        FacesContext context 
                = javax.faces.context.FacesContext.getCurrentInstance();
        HttpSession session 
                = (HttpSession) context.getExternalContext().getSession(false);
        ConectionBean conectionBean 
                =(ConectionBean) session.getAttribute("conectionBean");
        this.conn = conectionBean.currentConnection;
        
        currentRol = new Rol("", "");
        List< String > privSource = initPrivileges();
        List< String > privTarget = new ArrayList<>();
        
        privileges = new DualListModel<>(privSource, privTarget);
    }

    public ArrayList<String> initPrivileges() {
        FacesMessage msg = new FacesMessage();  
          
        ArrayList<String> res = new ArrayList<>();
        try {
            
            res = new RolBusiness( conn ).getPrivileges();
            
            msg.setSeverity(FacesMessage.SEVERITY_INFO);  
            //msg.setSummary("Privilegios");
            //msg.setDetail("Mostrados Exitosamente");  
            //FacesContext.getCurrentInstance().addMessage(null, msg);  
            return res;
        } catch (SQLException ex) {
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);  
            msg.setSummary("ERROR!");
            msg.setDetail("Fallo al cargar privilegios "+ex);  
            FacesContext.getCurrentInstance().addMessage(null, msg);  
        }
        return res;
    }
    
    public DualListModel<String> getPrivileges() {
        return privileges;
    }

    public void setPrivileges(DualListModel<String> privileges) {
        this.privileges = privileges;
    }

    public Rol getCurrentRol() {
        return currentRol;
    }

    public void setCurrentRol (Rol currentRol) {
        this.currentRol = currentRol;
    }

    public void onTransfer(TransferEvent event) {  
        StringBuilder builder = new StringBuilder();  
        for(Object item : event.getItems()) {  
            builder.append( (String) item );
        }  
          
        FacesMessage msg = new FacesMessage();  
        msg.setSeverity(FacesMessage.SEVERITY_INFO);  
        msg.setSummary("Items Transferred");
        msg.setDetail(builder.toString());  
          
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
    
    
    public void create(){
        FacesMessage msg = new FacesMessage();  
        
        if( !currentRol.getName().equals("") ){
            RolBusiness rb = new RolBusiness( conn );
            try {
                rb.create( currentRol.getName(), privileges.getTarget() );
                
                msg.setSeverity(FacesMessage.SEVERITY_INFO);  
                msg.setSummary("ROL Creado Exitosamente");
                msg.setDetail( currentRol.getName() +" creado.");  

                FacesContext.getCurrentInstance().addMessage(null, msg);  
                
                
                currentRol.setName("");
            } catch (SQLException ex) {
                
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);  
                msg.setSummary("Error al Crear");
                msg.setDetail( ex.getMessage() );  

                FacesContext.getCurrentInstance().addMessage(null, msg);  
                
                
                currentRol.setName("");
            }
        }
    }
}
